/*var mongoose=require('mongoose');
var config=require('../config');
var mongodb = require('mongodb');*/
var mongoose=require('./mongoConn');
var Schema=mongoose.Schema;
/*
var db=mongoose.createConnection(config.db_url);
db.on('error',function(error){
    db.close();
    console.log(error);
});

db.on('close', function () {
    db.open(config.db_url);
    console.log("重新连接mongo");
});
*/

var box_buyerSchema = new Schema({
    buyer_id: String,
    box_id:String
});

//var BoxBuyer = db.model("BoxBuyer", box_buyerSchema);
var BoxBuyer = mongoose.model("BoxBuyer", box_buyerSchema);

BoxBuyer.prototype.findBuyerById=function (boxid,callback){
    BoxBuyer.findOne({box_id:boxid}, function(err, datas){
        return callback(datas);
    });
}

BoxBuyer.prototype.findIdByBuyer=function (id,callback){
    BoxBuyer.find({buyer_id:id} ,function(err, datas){
        return callback(datas);
    });
}

BoxBuyer.prototype.findIdsByBuyer=function (id,callback){
    BoxBuyer.find({buyer_id:id},{box_id:1} ,function(err, datas){
        return callback(datas);
    });
}

BoxBuyer.prototype.removeBuyerById=function (boxid,callback){
    BoxBuyer.remove({box_id:boxid}, function(err){
        return callback(err);
    });
}

BoxBuyer.prototype.saveBuyerById=function(boxid,buyerid,callback){
    BoxBuyer.save({box_id:boxid},{buyer_id:buyerid},function(err){
        return callback(err);
    })
}


module.exports = BoxBuyer;